package com.anim.client;


import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;

public class MoonDeneme implements EntryPoint {
	
	private TabPanel tbpnl;
	private AbsolutePanel absPan;			// Permits abs. pos. of widgets
	private FocusPanel focPan;				// Permits mouse event capture
	private Image dukeImg;		// Duke and his shadow
	private Label Mx,My;
	private int scrollPosX=0;
	private int scrollPosY=0;
	private int ClickPositionX=0;
	private int ClickPositionY=0;
	
	public void onModuleLoad() {
		tbpnl=new TabPanel();
		tbpnl.setAnimationEnabled(true);
		tbpnl.setSize("800px", "600px");
		absPan = new AbsolutePanel();		// Create panel for absolute
		absPan.setSize("800px","600px");	// ...widget positioning
		Mx=new Label("X");
		My=new Label("Y");
		
		focPan = new FocusPanel();			// Place it inside a panel for
		focPan.add(absPan);					// ...mouse event capture
	
		focPan.addMouseListener(new DukeHandler());
		dukeImg = new Image("./duke.gif");		
		_add(dukeImg,0,120);
		tbpnl.add(new HTML("deneyiac"),"index");
		tbpnl.add(focPan,"Dene");
		tbpnl.selectTab(0);

		RootPanel.get("main").add(tbpnl);		// Hook it all up to page
		RootPanel.get("x").add(Mx);
		RootPanel.get("y").add(My);
		new AnimationTimer().scheduleRepeating(1); // Start anim loop
	}

	private void _add(Widget w,int x,int y)
	{	
		absPan.add(w);  
		absPan.setWidgetPosition(w,x,y);
	}
	
	private class DukeHandler extends MouseListenerAdapter
	{	
		public void onMouseUp(Widget w,int x,int y) {
			updateDuke(x,y);
			
		}
		
	}
	private void updateDuke(int x,int y)			// Update Duke / shadow alpha
	{	
		if(y>10 && y<590 && x>10 && x<790){	
//			absPan.setWidgetPosition(dukeImg,x,y);
			ClickPositionX=x;
			ClickPositionY=y;
		}
		Mx.setText("Px:"+scrollPosX);
		My.setText("Py:"+scrollPosY);
	}
	
	private class AnimationTimer extends Timer
	{	public void run()
		{	
			if(ClickPositionX>scrollPosX)
			{
				scrollPosX=(++scrollPosX)%800;
			}
			if(ClickPositionX<scrollPosX)
			{
				scrollPosX=(--scrollPosX)%800;
			}
			if(ClickPositionY>scrollPosY)
			{
				scrollPosY=(++scrollPosY)%800;
			}
			if(ClickPositionY<scrollPosY)
			{
				scrollPosY=(--scrollPosY)%800;
			}
	
			
			absPan.setWidgetPosition( dukeImg, 0+scrollPosX, 0+scrollPosY );
		}
	}
}
